//
//  HomeViewController.swift
//  TinyShopKeeper
//
//  Created by rimi on 16/5/25.
//  Copyright © 2016年 yuying. All rights reserved.
//

import UIKit

class HomeViewController: UITableViewController {
    
    var arrData:[[String:String]]?//数据
    
    var datasource:[[String:String]] = []
    var subfixString:String = ""//顶部titel
    
    //MARK:-刷新数据refreshData
    func refreshData(){
        print("ssssssss","\(UserInfo.userShop?.shop_name)!")//应该是登录时网络请求下来的数据，暂无
        self.title = (UserInfo.userShop?.shop_name)! + "-" + self.subfixString//用户信息存在UserInfo这里面
        
        self.tableView .reloadData()//tableView只要设置或者更改了数据源就必须刷新数据源
    }
    

    
    
    
    //MARK:-- Action
    //抽屉显隐
    @IBAction func showLeftOrNot(sender: UIBarButtonItem) {
        
        let mainVc = self.parentViewController?.parentViewController as! MainViewController //homevc的父控制器是自己的导航控制器，导航控制器的父控制器是mainvc
        mainVc.showLeftOrHidden()
    }
    
    //注销按钮~
    @IBAction func logOut(sender: UIBarButtonItem) {
        
        let mainVc = self.parentViewController?.parentViewController as! MainViewController //homevc的父控制器是自己的导航控制器，导航控制器的父控制器是mainvc
        mainVc.logOut()//注销方法是MainVc中的子视图控制器操作，毕竟HomeVc等级次于MainVc
        
        
        
    }
    
    //MARK:-- LifeCycle
    override func viewDidLoad() {
        super.viewDidLoad()

        //数据处理
        arrData = self.dataInfo()
        
        tableView.estimatedRowHeight = 130
        tableView.rowHeight = 130
     
    }

    

    // MARK: - Table view data source
    // MARK: -- UITableViewDataSource,UITableViewDelegate--

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 5
    }

    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       
        let cell = tableView.dequeueReusableCellWithIdentifier("HomeCell", forIndexPath: indexPath) as! HomeCell
        let data = arrData![indexPath.row]

        //cell的图标label等
        cell.leftImage.image = imageByName(data["imageName"]!)
        cell.totalLabel.text = data["title"]
        cell.detailLableOne.text = data["leftTitle"]
        cell.datailLableTwo.text = data["rightTitle"]
        cell.rightImage.image = UIImage(named: data["imageArrowName"]!)
        return cell
        
    }
    

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //从故事板中获取到详情页面

        if indexPath.row == 0{
            print("进入时段收入分析")
            let indentifier = self.datasource[indexPath.row]["class"]
            self.performSegueWithIdentifier(indentifier!, sender: self.tableView.cellForRowAtIndexPath(indexPath))
            
        }
        if indexPath.row == 3{
              print("进入会员列表页")
            let indentifier = self.datasource[indexPath.row]["class"]
            self.performSegueWithIdentifier(indentifier!, sender: self.tableView.cellForRowAtIndexPath(indexPath))
          
        }
        
        if indexPath.row == 4{
            print("sugueIdOfHistory")
            let indentifier = self.datasource[indexPath.row]["class"]
            self.performSegueWithIdentifier(indentifier!, sender: self.tableView.cellForRowAtIndexPath(indexPath))
        }
        
    }
    
    //MARK:--
    func dataInfo()->[[String:String]]{
        let dic1:[String:String] = ["imageName":"hourTime.png","title":"时段收入分析","leftTitle":"计时统计","rightTitle":"种类统计","imageArrowName":"op_red.png","class":"TinyShopKeeper_Swift.TimeIncomeViewController"]
        let dic2:[String:String] = ["imageName":"dailyBusiness.png","title":"日常运营分析","leftTitle":"销售额统计","rightTitle":"人均消费","imageArrowName":"op_blue.png","class":"TinyShopKeeper_Swift.TimeIncomeViewController"]
        let dic3:[String:String] = ["imageName":"shopBusiness.png","title":"商品运营分析","leftTitle":"销售额统计","rightTitle":"种类统计","imageArrowName":"op_yellow.png","class":"TinyShopKeeper_Swift.TimeIncomeViewController"]
        let dic4:[String:String] = ["imageName":"memberMessage.png","title":"会员信息","leftTitle":"历史消费单","rightTitle":"总消费","imageArrowName":"op_green.png","class":"TinyShopKeeper_Swift.TimeIncomeViewController"]
        let dic5:[String:String] = ["imageName":"historyRecord.png","title":"历史记录","leftTitle":"历史额统计","rightTitle":"总收入","imageArrowName":"op_purple.png","class":"TinyShopKeeper_Swift.TimeIncomeViewController"]
        let arr = [dic1,dic2,dic3,dic4,dic5]
        return arr

    }

}
